package com.cxhc.gank;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

public class FuliAdapter extends RecyclerView.Adapter<FuliAdapter.FuliViewHolder> {

    Context mContext;

    FuliBean data;

    @NonNull
    @Override
    public FuliViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.item_fuli, parent, false);
//        View view = View.inflate(mContext,R.layout.item_fuli,null);
        return new FuliViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final FuliViewHolder holder, final int position) {
//            holder.imageView
        if (data != null) {
            String url = data.getResults().get(position).getUrl();
            Glide.with(mContext).load(url).into(holder.imageView);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext,"点击了第 "+position,Toast.LENGTH_SHORT).show();

                    clickCallback.onClickPic(position,data,holder.imageView);
                }
            });
        }
//        holder.textView.setText("11111");
    }

    @Override
    public int getItemCount() {
        return dataSize;
    }


    int dataSize = 30;

    public void setData(FuliBean fuliBean) {
        //添加数据
        data = fuliBean;
        dataSize = fuliBean.getResults().size();
        notifyDataSetChanged();
    }

    class FuliViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

//        TextView textView;

        public FuliViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
//            textView = itemView.findViewById(R.id.tv);
        }
    }


    interface OnPicClickCallback{
        void onClickPic(int pos, FuliBean bean, ImageView imageView);
    }

    OnPicClickCallback clickCallback;

    public OnPicClickCallback getClickCallback() {
        return clickCallback;
    }

    public void setClickCallback(OnPicClickCallback clickCallback) {
        this.clickCallback = clickCallback;
    }
}
